<?php
/**
 * Created by PhpStorm.
 * User: De Niro
 * Date: 2015/5/22
 * Time: 15:19
 */
require_once("../api.php");
session_start();

if($_SERVER['REQUEST_METHOD'] != 'POST') {
    ajaxJSON(false,'method_error');
}else{
    $db = new SQLite3('../chatroom.db');//连接数据库
    $user_id = $_SESSION['user']['user_id'];
    $sql = "select * from user_info where user_id = $user_id";
    $result = $db ->query($sql);
    $changeItem = $_POST['changeItem'];
    $newValue = $_POST['newValue'];
    if($changeItem == 'nickName'){
        $sql = "update `user` set nickname = '$newValue' where  user_id = $user_id";
        $db ->query($sql);
        $_SESSION['user']['nickName'] = $newValue;
        ajaxJSON( true, "success" ,$sql);
    }else{
        if( $row = $result -> fetchArray(SQLITE3_ASSOC) ) {
            switch ($changeItem){
                case 'motto':
                    $editSql = "motto = '$newValue'";
                    break;
                case 'age':
                    $editSql = "age = $newValue";
                    break;
                case 'position':
                    $editSql = "position = '$newValue'";
                    break;
                case 'public':
                    $editSql = "public = $newValue";
                    break;
            }
            $sql = "update `user_info` set " . $editSql . " where user_id = $user_id";
            $db ->query($sql);
            ajaxJSON( true, "success" ,$sql );
        }else{
            switch ($changeItem){
                case 'motto':
                    $editSql = "VALUES ($user_id,'$newValue',null,null,1)";
                    break;
                case 'age':
                    $editSql = "VALUES ($user_id,null,$newValue,null,1)";
                    break;
                case 'position':
                    $editSql = "VALUES ($user_id,null,null,$newValue,1)";
                    break;
                case 'public':
                    $editSql = "VALUES ($user_id,null,null,null,$newValue)";
                    break;
            }
            $sql = 'insert into `user_info` ' . $editSql;
            $db ->query($sql);
            ajaxJSON( true, "success" ,$sql);
        }
    }




}
